package com.company;

public class Test01斐波那契数列 {
    public static void main(String[] args) {
        for (int i =0;i<10;i++){
            System.out.println(feiBo2(i));
        }
    }

    /**
     * 斐波那契数列
     * @param x
     * @return
     */
    public static int feiBo(int x){
        if(x<=1){
            return 1;
        }
        else{
            return feiBo(x-1)+feiBo(x-2);
        }
    }

    public static int feiBo2(int x){
        int f0 =1,f1=1,fn=0;
        if(x<=1){
            return 1;
        }else{
            for(int i=2;i<=x;i++){
                fn=f0+f1;
                f0=f1;
                f1=fn;
            }
        }
        return fn;
    }
}
